﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChatForum.SOAP.ServiceContract;
using ChatForum.Domain;
using ChatForum.DataContract;
using System.ServiceModel;

namespace ChatForum.Service.SOAP
{
	public class ForumService : ServiceBase, IForumService
	{
		public ForumService(BusinessLogic logic)
			: base(logic)
		{ }

		public void CreateCategory(CategoryDTO category)
		{
			try
			{
				logic.CreateCategory(category.Convert());
			}
			catch (Exception ex)
			{
				throw new FaultException<ChatForumFault>(new ChatForumFault() { FaultMessage = ex.Message });
			}
		}

		public void CreatePost(PostDTO post)
		{
			try
			{
				logic.CreatePost(post.Convert());
			}
			catch (Exception ex)
			{
				throw new FaultException<ChatForumFault>(new ChatForumFault() { FaultMessage = ex.Message });
			}
		}

		public CategoryDTO GetCategoryById(Guid catId)
		{
			try
			{
				return new CategoryDTO(logic.GetCategoryById(catId));
			}
			catch (Exception ex)
			{
				throw new FaultException<ChatForumFault>(new ChatForumFault() { FaultMessage = ex.Message });
			}
		}

		public PostDTO GetPostById(Guid postId)
		{
			try
			{
				return new PostDTO(logic.GetPostById(postId));
			}
			catch (Exception ex)
			{
				throw new FaultException<ChatForumFault>(new ChatForumFault() { FaultMessage = ex.Message });
			}
		}

		public IEnumerable<CategoryDTO> GetAllCategory()
		{
			try
			{
				return logic.GetAllCategory().Select(c => new CategoryDTO(c));
			}
			catch (Exception ex)
			{
				throw new FaultException<ChatForumFault>(new ChatForumFault() { FaultMessage = ex.Message });
			}
		}

		public IEnumerable<PostDTO> GetPostbyCategory(Guid categoryId, bool includeChildren)
		{
			try
			{
				return logic.GetPostbyCategory(categoryId, includeChildren).Select(c => new PostDTO(c));
			}
			catch (Exception ex)
			{
				throw new FaultException<ChatForumFault>(new ChatForumFault() { FaultMessage = ex.Message });
			}
		}

		public IEnumerable<PostDTO> GetPostbyCreator(string userId, bool includeChildren)
		{
			try
			{
				return logic.GetPostbyCreator(userId, includeChildren).Select(c => new PostDTO(c));
			}
			catch (Exception ex)
			{
				throw new FaultException<ChatForumFault>(new ChatForumFault() { FaultMessage = ex.Message });
			}
		}

		public IEnumerable<PostDTO> GetPostbyParentPost(Guid parentPostid, bool includeChildren)
		{
			try
			{
				return logic.GetPostbyParentPost(parentPostid, includeChildren).Select(c => new PostDTO(c));
			}
			catch (Exception ex)
			{
				throw new FaultException<ChatForumFault>(new ChatForumFault() { FaultMessage = ex.Message });
			}
		}


		public IEnumerable<PostDTO> GetThread(Guid postId)
		{
			try
			{
				return logic.GetThread(postId).Select(c => new PostDTO(c));
			}
			catch (Exception ex)
			{
				throw new FaultException<ChatForumFault>(new ChatForumFault() { FaultMessage = ex.Message });
			}
		}

		public void ModifyCategory(CategoryDTO category)
		{
			try
			{
				logic.ModifyCategory(category.Convert());
			}
			catch (Exception ex)
			{
				throw new FaultException<ChatForumFault>(new ChatForumFault() { FaultMessage = ex.Message });
			}
		}
	}
}
